﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiteExpress.Utilities.CryptoExtension
{
    public static class CryptoExtension
    {
        /// <summary>  
        /// 2进制字符串转16进制字符串
        /// </summary>  
        public static string ToHex(this byte[] bytes)
        {
            return BitConverter.ToString(bytes).Replace("-", "").ToLower();
        }

        public static string ToBase64(this byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }

        /// <summary>  
        /// 16进制字符串转2进制字符串
        /// </summary>  
        public static byte[] HexToBytes(this string hexString)
        {
            if ((hexString.Length % 2) != 0)
            {
                hexString += " ";
            }
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }
            return returnBytes;
        }

        public static byte[] Base64ToBytes(this string Base64str)
        {
            return Convert.FromBase64String(Base64str); ;
        }
    }
}
